﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace SingletonUnit
{
    //http://csharpindepth.com/Articles/General/Singleton.aspx
    //http://www.cnblogs.com/rush/archive/2011/10/30/2229565.html
    class Program
    {
        static void Main(string[] args)
        {
            Test(DelegateTestHandler.Instance.GetDelegateSingletonTest(SingletonType.Simple));
            RunAllSingletonTypeTest();
            Console.WriteLine("SingletonTest is complete,please press enter end current process ");
            Console.Read();
        }

        static void Test(DelegateSingletonTest delegateSingletonTest, int num = 10)
        {
            Thread[] th = new Thread[num];
            for (int i = 0; i < num; i++)
            {
                ThreadStartDelegateSingletonTest(th, i, delegateSingletonTest);
            }
            Thread.Sleep(100);
            Console.WriteLine("-----Thread Sleep 0.1 Seconds-----");
        }

        static void RunAllSingletonTypeTest()
        {
            Array arrs = Enum.GetValues(typeof(SingletonType));
            foreach (var arr in arrs)
            {
                Test(DelegateTestHandler.Instance.GetDelegateSingletonTest((SingletonType)arr));
            }
        }
        static void ThreadStartDelegateSingletonTest(Thread[] th, int index, DelegateSingletonTest delegateSingletonTest)
        {
            Thread n = new Thread(new ThreadStart(delegateSingletonTest));
            th[index] = n;
            th[index].Start();
        }
    }
}
